// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129
const db = uniCloud.database()
const dbCmd = db.command;
module.exports = {
	_before: function () { // 通用预处理器
		this.dbJQL = uniCloud.databaseForJQL({
			clientInfo: this.getClientInfo()
		})
	},
	// 获取学科列表
	async getSubject() {
		return await this.dbJQL.collection('uni-cms-categories').field('name, _id as id').get()
	},
	// 获取教师列表
	async getTeacher(params = {}) {
		const {
			subject_id = '',
			query,
			pageNum=1,
			pageSize=6
		} = params;

		let skipNum = pageSize*(pageNum -1);
		
		let teacherTemp = this.dbJQL.collection('teacher').skip(skipNum).limit(pageSize).getTemp();
		let subjectTemp = this.dbJQL.collection('uni-cms-categories').field('name, _id as id').getTemp();
		
		let newWhere = {}
		if(query && query.trim()) {
			newWhere.username = new RegExp(query, 'i')
		}
		// if(subject_id) {
		// 	 newWhere['subject_id._id'] = subject_id
		// }
	
		return await this.dbJQL.collection(teacherTemp, subjectTemp).where({
			...newWhere
			// subject_id: dbCmd.in(subject_id),
			// 'subject_id.value': subject_id,
			// username: new RegExp(query, 'i')
		}).get({
			getCount: true
		});
	}
	
}
